package com.css.fxfzysx.modules.lifeline.annotation;

import cn.hutool.core.util.StrUtil;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxRoadEntity;
import com.css.fxfzysx.util.PlatformObjectUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 校验是否有值
 */
public class ValidationProcessor {

    public static ArrayList<String> validate(Object obj) {
        List<Field> fields = getDeclaredFields(obj);
        ArrayList<String> list = new ArrayList<>();
        for (Field field : fields) {
            if (field.isAnnotationPresent(Validate.class)) {
                Validate validate = field.getAnnotation(Validate.class);
                String fieldName = validate.message();
                try {
                    field.setAccessible(true);
                    Object value = field.get(obj);
                    if (StrUtil.isBlankIfStr(value)) {
                        list.add(fieldName+"字段为空");
                    }
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return list;
    }

    private static List<Field> getDeclaredFields(Object obj) {
        Class<?> clazz = obj.getClass();
        return Arrays.asList(clazz.getDeclaredFields());
    }
}